package com.ddx.controller.core;

import com.ddx.common.exception.ExceptionUtil;
import com.ddx.common.exception.ResultModel;
import com.ddx.common.util.DataUtil;
import com.ddx.dto.ShoppingOrderVo;
import com.ddx.pay.MchOrderInfoResult;
import com.ddx.service.IUserPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: ${DESCRIPTION}
 * @date 2018/12/13
 */
@Api(value="用户支付",tags={"用户支付操作接口"})
@RestController
@RequestMapping(value = "/app")
public class UserPayController {
    private static Logger logger = LoggerFactory.getLogger(UserPayController.class);
    @Autowired
    IUserPayService iUserPayService;
    /**
     * 微信支付统一下单接口
     * @param shoppingOrderVo
     * @return
     */
    @ApiOperation(value = "微信支付统一下单接口", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/weixin/weixinPay", method = { RequestMethod.POST, RequestMethod.GET })
    public ResultModel weixinPay(@RequestBody ShoppingOrderVo shoppingOrderVo, HttpServletRequest request){
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            shoppingOrderVo.setUserId(request.getHeader("userId"));
            Map<String,String> weChatPayDto = iUserPayService.WeChatAppPay(shoppingOrderVo, DataUtil.getIpAdrress(request));
            resultModel.setData(weChatPayDto);
        }catch (Exception e){
            logger.error("微信支付统一下单接口异常",e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    /**
     * 微信支付缴纳保证金
     * @param shoppingOrderVo
     * @return
     */
    @ApiOperation(value = "微信支付统一下单接口", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/weixin/bondPay", method = { RequestMethod.POST, RequestMethod.GET })
    public ResultModel bondPay(@RequestBody ShoppingOrderVo shoppingOrderVo, HttpServletRequest request){
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            shoppingOrderVo.setUserId(request.getHeader("userId"));
            shoppingOrderVo.setAmount(new BigDecimal("600"));
            Map<String,String> weChatPayDto = iUserPayService.bondPay(shoppingOrderVo, DataUtil.getIpAdrress(request));
            resultModel.setData(weChatPayDto);
        }catch (Exception e){
            logger.error("微信支付统一下单接口异常",e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    /**
     * 微信支付统一下单接口
     * @param shoppingOrderVo
     * @return
     */
    @ApiOperation(value = "微信支付返回查询结果接口", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/weixin/callBack", method = { RequestMethod.POST, RequestMethod.GET })
    public ResultModel callBack(@RequestBody ShoppingOrderVo shoppingOrderVo, HttpServletRequest request){
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            shoppingOrderVo.setUserId(request.getHeader("userId"));
            MchOrderInfoResult mchOrderInfoResult=iUserPayService.callBack(shoppingOrderVo);
            resultModel.setData(mchOrderInfoResult);
        }catch (Exception e){
            logger.error("微信支付返回查询结果接口异常",e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    /**
     * 微信支付保证金缴纳返回
     * @param shoppingOrderVo
     * @return
     */
    @ApiOperation(value = "微信支付返回查询结果接口", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/weixin/bondCallBack", method = { RequestMethod.POST, RequestMethod.GET })
    public ResultModel bondCallBack(@RequestBody ShoppingOrderVo shoppingOrderVo, HttpServletRequest request){
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            shoppingOrderVo.setUserId(request.getHeader("userId"));
            MchOrderInfoResult mchOrderInfoResult=iUserPayService.bondCallBack(shoppingOrderVo);
            resultModel.setData(mchOrderInfoResult);
        }catch (Exception e){
            logger.error("微信支付返回查询结果接口异常",e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
}
